<script>
export default {
	onLaunch: function() {
    let gps=[]
    wx.setKeepScreenOn({
      keepScreenOn: true
    })
    //TODO
    wx.startLocationUpdate({
      success(resp){
          console.log('开启定位成功');
      },
      fail(resp){
        console.log('开启定位失败');
        uni.$emit('updateLocation',null);
      }
    })
      wx.onLocationChange(function(resp){
        let latitude=resp.latitude
        let longitude=resp.longitude
        let speed=resp.speed

        let location={
          latitude:latitude,
          longitude:longitude,

        }
        let workStatus=uni.getStorageSync("workStatus")
        workStatus="开始接单"
        let baseUrl='http://10.31.15.7:8201/hxds-driver';
        if(workStatus=="开始接单"){





        let settings=uni.getStorageSync("settings")
        settings={
              orderDistance:0,
              rangeDistance:5,
              orientation:''
          }
          let orderDistance=settings.orderDistance
          let rangeDistance=settings.rangeDistance
          let orientation=settings.orientation
          uni.request({
            url:`${baseUrl}/driver/location/updateLocationCache`,
            method:'POST',
            header:{
              token:uni.getStorageSync("token")
            },
            data:{
              latitude:latitude,
              longitude:longitude,
              speed:speed,
              orderDistance:orderDistance,
              rangeDistance:rangeDistance,
              orientLongitude:orientation!=""?orientation.longitude:null,
              orientLatitude:orientation!=""?orientation.latitude:null
            },
            success:function (resp){
                if(resp.statusCode==401){
                    uni.redirectTo({
                      url:"/pages/login/login"
                    })
                }
                else if(resp.statusCode==200&&resp.data.code==200){
                    let data=resp.data
                    if(data.hasOwnProperty("token")){
                        let token=data.token
                        uni.setStorageSync("token",token)
                    }
                    console.log("定位更新成功")
                }else {
                  console.error('更新GPS定位信息失败',resp.data);
                }
            },
            fail:function (resp){
                console.error('更新GPS定位信息失败',resp);
            }
          });
        }
        else if(workStatus=="开始代驾"){

        }
        uni.$emit('updateLocation',location);
      })
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	}
};
</script>

<style lang="scss">
@import 'uview-ui/index.scss';
</style>
